<?php

namespace app\home\controller;

use controller\BasicWechat;
use app\comm\service\UserOrderService;
use app\comm\service\UserService;
use app\comm\service\GoodsService;
use think\Controller;
use app\comm\controller\PcIndexController;
use think\Config;
use think\Db;
use app\comm\service\SysToolsLevePoint;
use app\comm\service\MyYcardService;
//订单类控制器
class Activiy extends PcIndexController {

	/**
	 *控制器前期构造出来的东西,是该控制器必须的东西
	 * @var [type]
	 */
	protected $systpoint;
	public function __constr_metho(){
		$this->systpoint = new SysToolsLevePoint();
	}
	/**
	 * 获取所有可以领取的抽奖券
	 * @return [type] [description]
	 */
	public function index(){
		$store_id = request()->get("store_id"); 
		$coupon = Db::table("ys_store_coupon")->select();
		// $sy = new SysToolsLevePoint();
		// $this->map['status'] =['eq',1];
		// $this->map['send_end_time'] =['GT',time()];
		// $res = $sy->precoupoulist($this->map);
		return view('activiy/index',['list'=>$res]);
	}

	/**
	 * ajax 保存用户优惠券
	 */
	public function setuseractiveiy(){
		$id = request()->get("id");
		$userservice = new UserService();
		$coup = $this->systpoint->getbycoupid($id);
		$uid = $this->userinfo['uid'];
		$check  = $userservice->checkuidycard($id,$uid);
		if($check) return json(["status"=>3,"msg"=>"已经领取"]);
		$data = ['uid' 			=>$uid,
				 'coupon_id'	=>$coup['id'],
				 'name'			=>$coup['name'],
				 'used_time'	=>'',
				 'amount'		=>$coup['money'],
				 'end_time'		=>$coup['use_start_time'],
				 'use_time'		=>$coup['use_end_time']];

		$res = $userservice ->userycardadd($data);
		return json(["status"=>1,"msg"=>"领取成功"]);
	}

	/**
	 * 我的优惠券
	 */
	public function myycard(){
		$uid = $this->userinfo['uid'];
		$map=[  "uid"     	=> $uid,
				"used_time" => ''];		
		$card = Db::table('ys_user_ycard')->alias('ycard')->where($map)->select();
		foreach ($card as $key => &$value) {
			 $value['coupon'] = Coupon::get($value['coupon_id'])->toArray();
		}	
		return view('activiy/myycard',['list'=>$card]);
	}


	/**
	 * 配置测试配置
	 */
	public function cong(){

	}



}